package com.lxl.bt.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.lxl.bt.R;
import com.lxl.bt.bean.UserAccountBean;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ACache;
import com.lxl.bt.util.BitMapUtils;
import com.lxl.bt.util.ToastUtil;
import com.yanzhenjie.sofia.Sofia;
import com.yzq.zxinglibrary.encode.CodeCreator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by lvxinlong
 * on  充值
 * on  2021年4月8日23:53:38
 **/
public class ChongzhiActivity extends BaseActivity {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ivQr)
    ImageView ivQr;
    @BindView(R.id.tvCopy)
    TextView tvCopy;
    @BindView(R.id.tvSave)
    TextView tvSave;
    @BindView(R.id.etDh)
    EditText etDh;
    @BindView(R.id.etNum)
    EditText etNum;
    @BindView(R.id.tvHint)
    TextView tvHint;
    @BindView(R.id.tvCommit)
    TextView tvCommit;
    @BindView(R.id.tv_qr_code)
    TextView tvQrCode;
    private Context context;
    ACache aCache;
    UserAccountBean userAccountBean;
    private String[] titles = {"全部", "充值", "提现", "转账", "支付", "挖矿", "返佣"};
    private String[] activityTitles = {"USDT钱包", "BTC钱包", "ETH钱包", "LTC钱包", "FIL钱包", "电费钱包"};
    private String[] reqs = {"usdt", "btc", "eth", "ltc", "fil", ""};
    private int name;
    int shouxu = 0;
    private Bitmap qrCodeBitmap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chongzhi);
        ButterKnife.bind(this);
        //状态栏全透明全屏显示页面，布局能够延伸到状态栏和导航栏下方
        Sofia.with(this)
                .invasionStatusBar()
                .statusBarLightFont()
//                .statusBarDarkFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);
        userAccountBean = (UserAccountBean) getIntent().getSerializableExtra("data");
        name = getIntent().getIntExtra("name", 0);
        context = this;
        rlBack.setVisibility(View.VISIBLE);
        txtToolbarTitle.setText("充值");
        rlRight.setVisibility(View.GONE);
        aCache = ACache.get(this);
        tvHint.setText(String.format("仅支持接收%s，官方到账核算以实际到账数量（不包含手续费）", reqs[name]));

        rechargeAddress();


    }


    private void rechargeAddress() {
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .rechargeAddress(reqs[name]).
                enqueue(new NetCallback<Result<String>>() {
                    @Override
                    public void onSuccess(Result<String> result) {
                        Log.w("lxl", ">>>>" + result.isSuccess());
                        if (result.isSuccess()) {
                            qrCodeBitmap = CodeCreator.createQRCode(result.getData(), 400, 400, null);
                            ivQr.setImageBitmap(qrCodeBitmap);
                            tvQrCode.setText(result.getData());
                        } else {
                            ToastUtil.showToast(ChongzhiActivity.this, result.getMsg());
                        }

                    }

                    @Override
                    public void netLogin() {

                    }

                    @Override
                    public void onError(String errorStr) {
                        super.onError(errorStr);
                    }

                });
    }

    private void commit() {
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .recharge(reqs[name], etDh.getText().toString(), etNum.getText().toString()).
                enqueue(new NetCallback<Result<String>>() {
                    @Override
                    public void onSuccess(Result<String> result) {
                        Log.w("lxl", ">>>>" + result.isSuccess());
                        if (result.isSuccess()) {
                            ToastUtil.showToast(ChongzhiActivity.this, result.getMsg());
                            finish();
                        } else {
                            ToastUtil.showToast(ChongzhiActivity.this, result.getMsg());
                        }

                    }

                    @Override
                    public void netLogin() {

                    }

                    @Override
                    public void onError(String errorStr) {
                        super.onError(errorStr);
                        Log.w("lxl", ">>>>" + errorStr);
                        ToastUtil.showToast(ChongzhiActivity.this, errorStr);
                    }

                });
    }


    @OnClick({R.id.rlBack, R.id.tvCopy, R.id.tvSave, R.id.tvCommit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                finish();
                break;
            case R.id.tvCopy:
                String code = tvQrCode.getText().toString();
                if (TextUtils.isEmpty(code)) return;
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("simple text", code);
                clipboard.setPrimaryClip(clip);
                ToastUtil.showToast(this, "复制成功");
                break;
            case R.id.tvSave:
                code = tvQrCode.getText().toString();
                if (qrCodeBitmap == null) return;
                BitMapUtils.saveBmp2Gallery(qrCodeBitmap, code, this);
                break;
            case R.id.tvCommit:
                commit();
                break;
        }
    }
}
